package com.hrhx.duhongming.adapter.highcharts;

import java.util.List;
import java.util.Map;

import com.github.abel533.echarts.axis.CategoryAxis;
import com.github.abel533.echarts.axis.ValueAxis;
import com.github.abel533.echarts.json.GsonOption;
import com.github.duhongming.highcharts.series.Column;
import com.hrhx.duhongming.adapter.EchartsAdapter;
import com.hrhx.duhongming.bean.EchartsConfig;

public class HighChartsBar3DAdapter implements EchartsAdapter{
	
	public static final HighChartsBar3DAdapter me = new HighChartsBar3DAdapter();
	
	//line and bar的适配器入口
	public  GsonOption adapter(EchartsConfig echartsConfig,
			GsonOption gsonOption,
			List<Map<String,Object>> data){
		//HighChartsBar3DAdapter barAdapter = new HighChartsBar3DAdapter();
		gsonOption = me.xAxisAdapter(echartsConfig,gsonOption,data);
		gsonOption = me.yAxisAdapter(echartsConfig,gsonOption,data);
		gsonOption = me.seriesAdapter(echartsConfig,gsonOption,data);
		return gsonOption;
	}
	
	//x轴配置以及单位配置
	public GsonOption xAxisAdapter(EchartsConfig echartsConfig,GsonOption gsonOption,List<Map<String,Object>> data){
		//X轴单位和分类数据	
		CategoryAxis categoryAxis = new CategoryAxis();
		categoryAxis.title().text(echartsConfig.getXaxisName());
		// X轴数据封装并解析
		if(data != null && data.size() != 0){
			for (Map<String,Object> map : data) {
				//遍历Map的有关x:deX轴数据
				for (String key : map.keySet()) {
					if(key.startsWith("x:")){
						categoryAxis.categories(map.get(key));
					}
				}
			}
		}
		gsonOption.xAxis(categoryAxis);
		return gsonOption;
	}
		
	//Y轴单位配置
	public GsonOption yAxisAdapter(EchartsConfig echartsConfig,GsonOption gsonOption,List<Map<String,Object>> data){
		String[] unitNameArray = echartsConfig.getYaxisName().split(",");
		for (String s : unitNameArray) {
			ValueAxis valueAxis = new ValueAxis();
			//设置y轴标题
			valueAxis.title().text(echartsConfig.getYaxisName());
			gsonOption.yAxis(valueAxis.name(s));
		}
		return gsonOption;
	}
		
	//Y轴数据处理
	public GsonOption seriesAdapter(EchartsConfig echartsConfig,GsonOption gsonOption,List<Map<String,Object>> data){
		if(data != null && data.size() != 0){
			//获取第一个List中的Map中的值
			for (String key : data.get(0).keySet()) {
				String[] k = key.split(":");
				//图例组件：Y轴数据类型
				if(k[0].startsWith("y")){
					gsonOption.legend().data(k[1]);
				}
			}
		}
				
		for(Object legendData : gsonOption.legend().data()){
			Column column = new Column();
			//echarts-type图表类型
			if(data != null && data.size() != 0){
				for (Map<String,Object> map : data) {
					//每个图表的Y轴数据匹配
					for (String key : map.keySet()) {
						if(key.endsWith((String)legendData)){
							column.name((String)legendData).data(map.get(key));
						}
					}
				}
			}
			gsonOption.series(column);
		}
		return gsonOption;
	}
}
